<?php

namespace App\Admin\Controllers;

use App\Models\Comment;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;

class CommentsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.comments'))
            ->description('description')
            ->row(function (Row $row) {
                $row->column(7, $this->userComments());
                $row->column(5, $this->adminComments());
//                $row->column(6, $this->docs());
            });
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.comments'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.comments'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.comments'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function userComments()
    {
        $grid = new Grid(new Comment);
        $grid->setTitle(trans('admin.user_comments_list'));
        $grid->model()
            ->where('user_type', 'App\Models\User')
            ->select('comments.*', 'users.username', 'users.nick_name', 'users.avatar')
            ->join('users', 'user_id', 'users.id')
            ->orderBy('created_at', 'desc');
        $grid->id('Id');
        $grid->user(trans('admin.comment_user'))->display(function ($value) {
            return $value['username'];
        });
        $grid->username(trans('admin.username'));
        $grid->nick_name(trans('admin.nick_name'));
        $grid->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->content(trans('admin.content'))->limit(20)->display(function ($value) {
            return '<span class="badge bg-light-blue">' . $this->children->count() . '</span>' . ' ' . $value;
        })->expand(function ($model) {
            $comments = $model->children()->get()->map(function ($comment) {
                return $comment->only(['id', 'content', 'created_at']);
            });

            return new Table(['ID', trans('admin.content'), trans('admin.created_at')], $comments->toArray());
        });
        $grid->like_amount(trans('admin.like_amount'))->badge('blue');
        $grid->is_show(trans('admin.show'))->switch();
        $grid->created_at(trans('admin.created_at'));
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('username', trans('admin.username'));
                $filter->like('content', trans('admin.content'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('nick_name', trans('admin.nick_name'));
            });

        });

//        $grid->updated_at(trans('admin.updated_at'));

        return $grid;
    }

    protected function adminComments()
    {
        $grid = new Grid(new Comment);
        $grid->setTitle(trans('admin.admin_comments_list'));
        $grid->model()
            ->where('user_type', 'App\Models\AdminUser')
            ->select('comments.*', 'admin_users.username', 'admin_users.avatar')
            ->join('admin_users', 'user_id', 'admin_users.id')
            ->orderBy('created_at', 'desc');
        $grid->id('Id');
        $grid->user(trans('admin.comment_user'))->display(function ($value) {
            return $value['username'];
        });
        $grid->username(trans('admin.username'));
        $grid->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->content(trans('admin.content'))->limit(20)->display(function ($value) {
            return '<span class="badge bg-light-blue">' . $this->children->count() . '</span>' . ' ' . $value;
        })->expand(function ($model) {
            $comments = $model->children()->get()->map(function ($comment) {
                return $comment->only(['id', 'content', 'created_at']);
            });

            return new Table(['ID', trans('admin.content'), trans('admin.created_at')], $comments->toArray());
        });
        $grid->like_amount(trans('admin.like_amount'))->badge('blue');
        $grid->is_show(trans('admin.show'))->switch();
        $grid->created_at(trans('admin.created_at'));
//        $grid->updated_at(trans('admin.updated_at'));
        $grid->disableCreateButton();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disablePagination();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Comment::findOrFail($id));

        $show->username(trans('admin.comment_user'))->as(function () {
            return $this->user->username;
        });
        $show->nick_name(trans('admin.nick_name'))->as(function () {
            return $this->user->nick_name;
        });
        $show->avatar(trans('admin.avatar'))->as(function () {
            return $this->user->avatar;
        })->image();
        $show->content(trans('admin.content'))->unescape();
        $show->like_amount(trans('admin.like_amount'));
        $show->children(trans('admin.comments'), function ($data) {
            $data->setResource('/comments');
            $data->id('Id');
            $data->user()->username(trans('admin.comment_user'));
            $data->user()->nick_name(trans('admin.nick_name'));
            $data->user()->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
            $data->content(trans('admin.content'))->limit(20);
            $data->like_amount(trans('admin.like_amount'));
            $data->disableCreateButton();

        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Comment);

        $form->display('content', trans('admin.content'));
        $form->switch('is_show', trans('admin.show'));
        $form->hasMany('children', trans('admin.comments'), function (Form\NestedForm $form) {
            $form->textarea('content', trans('admin.content'));
            $form->hidden('user_id', 'username')->value(Admin::user()->id);
            $form->hidden('user_type', 'user_type')->value('App\Models\AdminUser');
        });
        $form->disableEditingCheck();

        $form->disableCreatingCheck();

        $form->disableViewCheck();

        $form->tools(function (Form\Tools $tools) {
//            $tools->disableDelete();
//            $tools->disableView();
//            $tools->disableList();
        });

        return $form;
    }
}
